#      t.string :name
#      t.integer :logged_controller_id


class LoggedAction < ActiveRecord::Base
	validates_presence_of :name, :logged_controller_id
	
	belongs_to :logged_controller
	has_many :log_entries
	
	class << self
		def find_or_create_by_name_and_controller_id(name, logged_controller_id)
			la = find_by_name_and_logged_controller_id name, logged_controller_id
			la || create( :name => name, :logged_controller_id => logged_controller_id )
		end
	end
	
	def count
		@count || LogEntry.count_by_logged_action_id(self.id)
	end
	
	def title_name
		name
	end
	
	# average time to complete this action in seconds
	def average_completion_time
		@average_completion_time ||= (LogEntry.average_times_for_action_id(self.id)[:completion] || 0.0)
	end
	
	# average time to render this action in seconds
	def average_render_time
		@average_render_time ||= (LogEntry.average_times_for_action_id(self.id)[:render] || 0.0)
	end
	
	# average db time for this action in seconds
	def average_db_time
		@average_db_time ||= (LogEntry.average_times_for_action_id(self.id)[:db] || 0.0)
	end
	
	# average time to complete this action in milliseconds
	def average_completion_time_in_ms
		(average_completion_time * 1000).to_i
	end
	
	# average time to render this action in milliseconds
	def average_render_time_in_ms
		(average_render_time * 1000).to_i
	end
	
	# average db time for this action in milliseconds
	def average_db_time_in_ms
		(average_db_time * 1000).to_i
	end
	
	
end
